package com.jd.myaaaccount.model;

import com.jd.myaaaccount.entity.Family;
import com.jd.myaaaccount.entity.parameter.CreateFamilyParameter;
import com.jd.myaaaccount.entity.parameter.JoinFamilyParameter;
import com.jd.myaaaccount.entity.result.GetCategoryResult;
import com.jd.myaaaccount.entity.result.GetFamilyResult;
import com.jd.myaaaccount.entity.result.RegistOrCreateResult;
import com.jd.myaaaccount.util.ErrorCode;
import com.jd.myaaaccount.util.HttpHelper;
import com.jd.myaaaccount.util.JsonHelper;
import com.jd.myaaaccount.util.OperateCode;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class FamilyModel {
	private Context context;

	public FamilyModel(Context context) {
		this.context = context;
	}

	public void create(Family family, Handler handler) {
		CreateFamilyParameter cfp = new CreateFamilyParameter();
		cfp.setFamily(family);
		cfp.setUser(Common.currentUser);
		cfp.setToken(Common.token);
		String params = JsonHelper.getString(cfp);
		String res = HttpHelper.postRequest("family/create", params);
		Message msg = new Message();
		msg.what = OperateCode.ADD;
		if (res == "") {
			msg.arg1 = ErrorCode.NET_TIMEOUT;
		} else {
			RegistOrCreateResult rcr = JsonHelper.getObject(res,
					RegistOrCreateResult.class);
			Common.resultToMessage(rcr, msg);
		}
		handler.sendMessage(msg);
	}

	/**
	 * 加入家庭
	 * 
	 * @param number
	 *            家庭ID
	 * @param password
	 *            家庭密码
	 * @param handler
	 */
	public void join(int number, String password, Handler handler) {
		JoinFamilyParameter jfp = new JoinFamilyParameter();

		jfp.setNumber(number);
		jfp.setPassword(password);
		jfp.setUser(Common.currentUser);
		jfp.setToken(Common.token);
		String params = JsonHelper.getString(jfp);
		String res = HttpHelper.postRequest("family/join", params);
		Message msg = new Message();
		msg.what = OperateCode.JOIN;
		if (res == "") {
			msg.arg1 = ErrorCode.NET_TIMEOUT;
		} else {
			RegistOrCreateResult rcr = JsonHelper.getObject(res,
					RegistOrCreateResult.class);
			Common.resultToMessage(rcr, msg);
		}
		handler.sendMessage(msg);
	}

	/**
	 * 获取我(当前用户)的家庭
	 * 
	 * @param handler
	 * @return
	 */
	public void getMyFamily(Handler handler) {
		String url = String.format(
				"family/getfamilybyuserid/?userId=%s&token=%s&detail=0",
				Common.currentUser.getId(), Common.token);
		String res = HttpHelper.getRequest(url);
		Message msg = new Message();
		msg.what = OperateCode.GET_FAMILY;
		if (res == "") {
			msg.arg1 = ErrorCode.NET_TIMEOUT;
		} else {
			GetFamilyResult gfr = JsonHelper.getObject(res,
					GetFamilyResult.class);
			Common.resultToMessage(gfr, msg);
		}
		handler.sendMessage(msg);
	}

	public void getCategory(String familyid, Handler handler) {
		String url = String.format(
				"family/getcategory/?userId=%s&token=%s&familyId=%s",
				Common.currentUser.getId(), Common.token, familyid);
		String res = HttpHelper.getRequest(url);
		Message msg = new Message();
		msg.what = OperateCode.GET_CATEGORY;
		if (res == "") {
			msg.arg1 = ErrorCode.NET_TIMEOUT;
		} else {
			GetCategoryResult gcr = JsonHelper.getObject(res,
					GetCategoryResult.class);
			Common.resultToMessage(gcr, msg);
		}
		handler.sendMessage(msg);
	}

}
